// my/my.js
import {
  InvioceModel
} from '../../../models/invioce'
import {
  PayModel
} from '../../../models/pay'
import {
  Format
} from '../../../utils/common'
const payModel = new PayModel()
const invioceModel = new InvioceModel()
const app = getApp()
let last
const format = new Format()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    items: [
      { name: '企业单位', value: '企业单位' , checked: true},
      { name: '个人/非企业单位', value: '个人/非企业单位' },
    ],
    invoiceDetail: {type: 1,state:0,gmfNsrsbh: '',},
    oneinvoiceDetail: {type: 1,state:0,gmfNsrsbh: '',},
    twoinvoiceDetail: {type: 2,state:0,gmfNsrsbh: '',},
    isAdd: true

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    if (options.id) {
      this.setData({
        'isAdd': false
      })
      my.setNavigationBar({
        reset: true,
        title: '编辑抬头',
      });
      this.invoiceTileDetail(options.id)
    } else {
      my.setNavigationBar({
        reset: true,
        title: '添加抬头',
      });
    }
   
    // this.setData({
    //   'invoiceDetail[type]': 1
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindKeyInput(e) {
    console.log(e)
    this.data.invoiceDetail[e.currentTarget.dataset.name] = e.detail.value
  },
  radioChange: function (e) {
    console.log(e)
    if (this.data.invoiceDetail.type == '1') {
      this.data.oneinvoiceDetail= this.data.invoiceDetail
      this.data.twoinvoiceDetail.state = this.data.invoiceDetail.state
      this.setData({
        'invoiceDetail': this.data.twoinvoiceDetail
      })
    } else {
      this.data.twoinvoiceDetail= this.data.invoiceDetail
      this.data.oneinvoiceDetail.state = this.data.invoiceDetail.state
      this.setData({
        'invoiceDetail': this.data.oneinvoiceDetail
      })
    }
    
    this.setData({
      'invoiceDetail[type]': e.detail.value
    })
  },
  switch2Change(e) {
    console.log(e)
    if (e.detail.value) {
      this.setData({
        'invoiceDetail[state]': 1,
      });
    } else {
      this.setData({
        'invoiceDetail[state]': 0,
      });
    }
    
  },
  invoiceTileDetail(id) {
    invioceModel.invoiceTileDetail(id).then(res => {
      console.log(res)
      if (res && res.code == '00') {
        this.setData({
          'invoiceDetail': res.invoiceTitle,
        });
      } else {
        if (res && res.msg) {
          my.showToast({
            type: 'none',
            content: res.msg,
            duration: 1000,
          });
        } else {
          my.showToast({
            type: 'none',
            content: '查询失败',
            duration: 1000,
          });
        }
      }
    })
  },
  saveInvoiceTitle () {
    let detail = this.data.invoiceDetail
    console.log(detail)
    if (!detail.name) {
      my.showToast({
        type: 'none',
        content: '请输入开票抬头',
        duration: 1000,
      });
      return
    }
    var regGmfNsrsbh = /^[A-Z0-9]{15}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/
    if (detail.type == 1 && !detail.gmfNsrsbh) {
      my.showToast({
        type: 'none',
        content: '请输入税号',
        duration: 1000,
      });
      return
    }
    if (detail.type == 1 &&!regGmfNsrsbh.test(detail.gmfNsrsbh)) {
      my.showToast({
        type: 'none',
        content: '税号不正确',
        duration: 1000,
      });
      return
    }
    invioceModel.saveInvoiceTitle(detail.id,detail.type,detail.name,detail.gmfNsrsbh,detail.gmfDz,detail.gmfDh,detail.gmfYh,detail.gmfYhzh,detail.remarks,1,detail.state).then(res => {
      console.log(res)
      if (res.code == '00') {
        if (this.data.isAdd) {
          my.showToast({
            type: 'none',
            content: '添加成功',
            duration: 1000,
            success: () => {
              my.navigateBack()
            },
          });
        } else {
          my.showToast({
            type: 'none',
            content: '保存成功',
            duration: 1000,
            success: () => {
              my.navigateBack()
            },
          });
        }
      } else {
        my.showToast({
          type: 'none',
          content: res.msg,
          duration: 1000,
        });
      }
    })
  },
  deleteInvoiceTitle () {
    invioceModel.deleteInvoiceTitle(this.data.invoiceDetail.id).then(res => {
      console.log(res)
      if (res.code == '00') {
        my.showToast({
          type: 'none',
          content: '删除成功',
          duration: 1000,
          success: () => {
            my.navigateBack()
          },
        });
        
      } else {
        my.showToast({
          type: 'none',
          content: '查询失败',
          duration: 1000,
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.loadMyAccount()
    this.setData({
        'invoiceDetail': {type: 1,state:0,gmfNsrsbh: '',},
    })
    console.log('1')
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})